package alo.p6.work4;

import alo.p6.BinarySearchTreeNode;

/**
 * @ClassName Find
 * @Description 在二叉搜素树中寻找元素
 * @Author Fanxc
 * @DATE 2021/2/10 10:08
 * @Version 1.0
 */
public class Find {
    //使用递归
    BinarySearchTreeNode find(BinarySearchTreeNode root,int data){
        if (root==null){
            return null;
        }
        if (data< root.getData()){
            return find(root.getLeft(),data);
        }else if (data> root.getData()){
            return find(root.getRight(),data);
        }
        return root;
    }
    //使用迭代
    BinarySearchTreeNode find2(BinarySearchTreeNode root,int data){
        if (root==null){
            return null;
        }
        while (root!=null){
            if (data== root.getData()){
                return root;
            }else if (data> root.getData()){
                root=root.getRight();
            }else {
                root=root.getLeft();
            }
        }
        return null;
    }
}
